package cn.net.awit.API.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.net.awit.API.entity.JSONResult;
import cn.net.awit.weaforcast.entity.WeaForcastEntity;
import cn.net.awit.weaforcast.service.WeaForcastServiceI;
import cn.net.awit.weastation.entity.WeaStationEntity;
import cn.net.awit.weastation.service.WeaStationServiceI;

/**   
 * @Title: Controller  
 * @Description: 获取station状态接口
 * @author onlineGenerator
 * @date 2018-11-20 04:33:37
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/weatherData")
public class WeatherDataController {
	private static final Logger logger = LoggerFactory.getLogger(APILoginController.class);
	
	@Autowired
	private WeaStationServiceI weaStationService;
	@Autowired
	private WeaForcastServiceI weaForcastService;
	@Autowired
	private SystemService systemService;
	
	/**
	 * 获取有效气象站的详细信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "getActivedStation")
	@ResponseBody
	public JSONResult getActivedStation(){
		JSONResult jsonResult = new JSONResult();
		try {
			List<WeaStationEntity> list = weaStationService.findByProperty(WeaStationEntity.class, "stationStatus", 1);
			jsonResult.addObj("data", list);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getActivedStation:err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	/**
	 * 获取有效预报点的详细信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "getActivedForcast")
	@ResponseBody
	public JSONResult getActivedForcast(){
		JSONResult jsonResult = new JSONResult();
		try {
			List<WeaForcastEntity> list = weaForcastService.findByProperty(WeaForcastEntity.class, "status", 1);
			jsonResult.addObj("data", list);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getActivedForcast:err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
	/**
	 * 获取预报点的某项数据
	 * 
	 * @param type 数据类型
	 * @return
	 */
	@RequestMapping(params = "forcastTypeData")
	@ResponseBody
	public JSONResult forcastTypeData(HttpServletRequest request){
		JSONResult jsonResult = new JSONResult();
		try {
			String type = request.getParameter("type");
			String[] fields = {"temp","rh","vis","ws","rain","snow","cloud"}; 
			String value = null;
			if(type != null && !"".equals(type)){
				value = fields[Integer.parseInt(type)-1];
				List<Map<String, Object>> findForJdbc = weaForcastService.findForJdbc("select b.*,a."+value+" as Value from wea_forcast_data as a,wea_forcast as b "
							+"where a.forcast_id=b.id and b.status=1 "
							+"and time>DATEADD(HOUR,-1,GETDATE()) and time<GETDATE()");
				jsonResult.setCode(200);
				jsonResult.addObj("data", findForJdbc);
			}else{
				jsonResult.setCode(203);
				jsonResult.setMsg("缺少参数");
			}
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/forcastTypeData:err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
	/**
	 * 获取预报点数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getWeatherData")
	@ResponseBody
	public JSONResult getWeatherData(HttpServletRequest request){
		JSONResult jsonResult = new JSONResult();
		try {
			String lat = request.getParameter("lat");
			String lng = request.getParameter("lng");
			String level = request.getParameter("level");
			int i = Integer.parseInt(level);
			int[] a = {1,12,72};
			String sql1 = "select top "+a[i-1]+" * from wea_forcast_data "
					+"where forcast_id=( "
					+"select id from wea_forcast where lat=? and lng=? "
					+") and time>DATEADD(HOUR,-1,GETDATE()) "
					+"order by time asc";
			List<Map<String, Object>> list = weaForcastService.findForJdbc(sql1, lat, lng);
			jsonResult.addObj("data", list);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getWeatherData:err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
	/**
	 * 获取预报点历史数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getHistoryData")
	@ResponseBody
	public JSONResult getHistoryData(HttpServletRequest request){
		JSONResult jsonResult = new JSONResult();
		try {
			String forcast_id = request.getParameter("forcast_id");
			String date = request.getParameter("date");
			List<Map<String, Object>> list = systemService.findForJdbc("exec proc_forcast_history @forcast_id= ?,@time=?",forcast_id,date);
			for (Map<String, Object> map : list) {
				Object object = map.get("time");
				String formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(object);
				map.put("time", formatter);
			}
			jsonResult.addObj("data", list);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getHistoryData:err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
	
	/**
	 * 获取气象站的最新数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getStationInfo")
	@ResponseBody
	public JSONResult getStationInfo(HttpServletRequest request){
		JSONResult jsonResult = new JSONResult();
		try {
			String station_code = request.getParameter("station_code");
			List<Map<String, Object>> list = 
					systemService.findForJdbc("select top 1 * from wea_station_data where station_code=? order by data_time desc",station_code);
			jsonResult.addObj("data", list);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getStationInfo：err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}

	/**
	 * 获取气象站历史数据
	 * 
	 * @param request
	 * @return
	 * 
	 * 
	 * 
	 * 
	 */
	@RequestMapping(params = "getStationHistory")
	@ResponseBody
	public JSONResult getStationHistory(HttpServletRequest request){
		JSONResult jsonResult = new JSONResult();
		try {
			String station_code = request.getParameter("station_code");
			String data_time = request.getParameter("data_time");
			List<Map<String, Object>> list = 
					systemService.findForJdbc("select * from wea_station_data where station_code=?"
							+ " and data_time>? and data_time<DATEADD(hour,1,?)",station_code,data_time,data_time);
			jsonResult.addObj("data", list);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getStationHistory：err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
	/**
	 * 获取预报点灾害数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getDisaster")
	@ResponseBody
	public JSONResult getDisaster(HttpServletRequest request,int type,int hour){
		JSONResult jsonResult = new JSONResult();
		try {
			List<Map<String, Object>> list = systemService.findForJdbc("exec proc_dis_statu @forcast_id=?,@disaster=?,@hour=?,@when=?",
					null,type,null,hour);
			jsonResult.addObj("data", list);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getDisaster:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
	
	/**
	 * 获取48h的echarts数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getStationEchart")
	@ResponseBody
	public JSONResult getStationEchart(HttpServletRequest request,int type,String stationCode){
		JSONResult jsonResult = new JSONResult();
		try {
			Date date = new Date();
			String time = new SimpleDateFormat("yyyy-MM-dd HH:00:00").format(date);
			List<Map<String, Object>> list = systemService.findForJdbc("exec proc_station_echart @stationCode=?, @type=?,@time=?,@hour=?"
					,stationCode,type,time,48);
			List<String> xArr=new ArrayList<>();
			List<Double> yArr=new ArrayList<>();
			for(int i=0;i<list.size();i++){
				Object xtObj = list.get(i).get("xt");
				Object valObj = list.get(i).get("val");
				if(xtObj!=null && valObj!=null){

					Date xt = (Date)xtObj;
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(xt);
					Double val = Double.parseDouble(valObj.toString());
					int hours=calendar.get(Calendar.HOUR_OF_DAY);
					int day=calendar.get(Calendar.DAY_OF_MONTH);
					int month=calendar.get(Calendar.MONTH)+1;
					if(hours==0){
						xArr.add(hours+"/"+month+"."+day);
					}else{
						xArr.add(hours+"");
					}

					yArr.add(val);
				}
			}
			jsonResult.addObj("xData", xArr);
			jsonResult.addObj("yData", yArr);
			jsonResult.addObj("title", new SimpleDateFormat("yyyy-MM-dd").format(date));
			jsonResult.setCode(200);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("weatherData/getStationEchart:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
}
